﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

namespace WebApplication1.masterpage
{
    public partial class User : System.Web.UI.MasterPage
    {

        protected void Page_Init(object sender, EventArgs e)
        {
            if (Context.Session["User"] == null)
            {
                Response.Redirect("~/Default.aspx");
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            Business.User userobj = (Business.User)Session["User"];

            // Default admin links and cafe links are disable
            List<fos_UserLinkRole> userRoles = new List<fos_UserLinkRole>();
            userRoles = userobj.userRoles;
            // Role Id 1 is admin, Role ID 3 is Cafetaria, Role Id 2 is Employee
            int adminCount = userRoles.Count(a => a.RoleId == 1);
            int CafeCount = userRoles.Count(a => a.RoleId == 3);
            if (adminCount >= 1)
                AdminTab.Visible = true;
            if (CafeCount >= 1)
                CafeTab.Visible = true;
            if (userobj != null)
            {
                userLabel.Text = userobj.email;
            }
            else
            {
                // If Session object is empty, forcing to first page
                Response.Redirect("~/Default.aspx");
                userLabel.Text = "Anonymous User";
            }

            // To prevent access by typing the pagename directly.  

            // need to ignore case
            string cp = Page.ToString().Replace("ASP.", "").Replace("_", ".");

            if (string.Equals(cp, "AddCaterer.aspx", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(cp, "AddSite.aspx", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(cp, "PrinterStatus.aspx", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(cp, "Settings.aspx", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(cp, "userlist.aspx", StringComparison.OrdinalIgnoreCase))
            {
                if (adminCount < 1) Response.Redirect("~/Default.aspx");
            }
            if (string.Equals(cp, "AddFood.aspx", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(cp, "ActiveOrder.aspx", StringComparison.OrdinalIgnoreCase))
            {
                // shud be checking cafecount only
                //if (adminCount < 1 && CafeCount < 1) Response.Redirect("~/Default.aspx");  
                if (CafeCount < 1) Response.Redirect("~/Default.aspx");
            }

        }

        protected void bnkLogout_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Session.Clear();
            Response.Redirect("~/Logout.aspx");
        }
    }
}